package com.market.dao.jpa;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.market.dao.OrderDetailDao;
import com.market.dao.ProductDao;
import com.market.domain.OrderDetail;
import com.market.domain.Product;
import com.market.exception.OutOfStockException;

@Repository("orderDetailDao")
public class OrderDetailDaoJpa extends GenericDaoJpa<OrderDetail, Long>
		implements OrderDetailDao {
	@Autowired
	private ProductDao productDao;

	public OrderDetailDaoJpa() {
		super(OrderDetail.class);
	}

	public void remove(OrderDetail object) {
		if (super.findById(object.getId()) == null) {
			log.debug(this.getClass() + " Order detail with id "
					+ object.getId() + " is not found, can not be remove");
		} else {
			log.debug(this.getClass() + " Order detail with id "
					+ object.getId() + " is found, will be remove");
			super.removeById(object.getId());
		}
	}
	
	

	public OrderDetail saveAndUpdateInventory(OrderDetail orderDetail) {
		Product product = this.productDao.findById(orderDetail.getProduct()
				.getId());
		if ((product.getInventory() - orderDetail.getConfirmedQuantity()) >= 0) {
			product.setInventory(product.getInventory()
					- orderDetail.getConfirmedQuantity());

			this.productDao.save(product);
			orderDetail.setProduct(product);
			orderDetail = super.save(orderDetail);
		} else {
			throw new OutOfStockException(product.getNumber(),
					product.getName(), product.getInventory());
		}
		return orderDetail;
	}

	/**
	 * Confirm one orderDetail, minus confirmedQuantity from inventory.
	 */
	
	public OrderDetail confirm(OrderDetail orderDetail) {
		Product product = this.productDao.findById(orderDetail.getProduct()
				.getId());
		if ((product.getInventory() - orderDetail.getConfirmedQuantity()) >= 0) {
			product.setInventory(product.getInventory()
					- orderDetail.getConfirmedQuantity());
			product = this.productDao.save(product);
			orderDetail.setProduct(product);
		} else {
			throw new OutOfStockException(product.getNumber(),
					product.getName(), product.getInventory());
		}
		return orderDetail;
	}

	/**
	 * Remove an order detail from database, and restore confirmedQuantity back to inventory.
	 */
	public void removeAndUpdateInventory(OrderDetail orderDetail) {
		Product product = this.productDao.findById(orderDetail.getProduct()
				.getId());
		product.setInventory(product.getInventory()
				+ orderDetail.getConfirmedQuantity());
		this.productDao.save(product);
		super.removeById(orderDetail.getId());
	}

	/**
	 * Unconfirmed orderdetail, restore confirmedQuantity back to inventory, and
	 * set confirmedQuantity and confirmedPrice back to zero;
	 */
	public OrderDetail unConfirm(OrderDetail orderDetail) {
		Product product = productDao.findById(orderDetail.getProduct().getId());
		product.setInventory(product.getInventory()	+ orderDetail.getConfirmedQuantity());
		product = productDao.save(product);
		orderDetail.setProduct(product);
		orderDetail.setConfirmedQuantity(0);
		orderDetail.setConfirmedPrice(0D);		
		return super.save(orderDetail);
	}

}
